#include "TriggerRoad.h"
#include "GeomSvc.h"

ClassImp(TriggerRoad)
ClassImp(Trigger)

using namespace std;

void TriggerRoad::clear()
{
  roadID = -1;
  targetWeight = 0.;
  dumpWeight = 0.;

  detectorIDs.clear();
  elementIDs.clear();

  isEnabled = true;
}

void TriggerRoad::addElement(int detectorID, int elementID)
{
  detectorIDs.push_back(detectorID);
  elementIDs.push_back(elementID);
}

bool TriggerRoad::isValid()
{
  return 1;
}


bool TriggerRoad::operator==(const TriggerRoad& elem) const
{
  if(detectorIDs.size() != elem.detectorIDs.size()) return false;

  int nElements = detectorIDs.size();
  for(int i = 0; i < nElements; i++)
    {
      if(detectorIDs[i] != elem.detectorIDs[i]) return false;
      if(elementIDs[i] != elem.elementIDs[i]) return false;
    }

  return true;
}

int TriggerRoad::getLR()
{
  GeomSvc* p_geomSvc = GeomSvc::instance();
  int TB = 0;
  int LR = 0;
  for(int i = 1; i < 4; i++)
    {
      //  cout <<"H"<<i+1<<":"<< elementIDs[i] <<" middle:"<< p_geomSvc->getPlaneNElements(detectorIDs[i])/2<< endl;
      if(elementIDs[i] <= p_geomSvc->getPlaneNElements(detectorIDs[i])/2)// elementID<= 8 or 11
	{
	  LR += 1;   //right
	}
      else
	{
	  LR += -1;  //left
	}
    }


  for(int i = 0; i < 4; i++)
    {
      if((detectorIDs[i] & 1) == 0)
	{
	  TB += 1;
	}
      else
	{
	  TB += -1;
	}
    }


  if ((TB!=4) && (TB!=-4)) return 0;

  if(LR == 3) return 1;
  if(LR == -3) return -1;
  return 0;
}

int TriggerRoad::getRevH1LR()
{
  GeomSvc* p_geomSvc = GeomSvc::instance();
  int LR = 0;
  //  cout <<"H1:"<< elementIDs[0] <<" middle:"<< p_geomSvc->getPlaneNElements(detectorIDs[0])/2<< endl;
  if(elementIDs[0] <= p_geomSvc->getPlaneNElements(detectorIDs[0])/2)  //middle H1elementID <=11  
    {
      LR += -1; //right
    }
  else
    {
      LR += 1;  //left
    }

  for(int i = 1; i < 4; i++)
    {
      if(elementIDs[i] <= p_geomSvc->getPlaneNElements(detectorIDs[i])/2)
	{
	  LR += 1;  //right
	}
      else
	{
	  LR += -1;  //left
	}
    }




  if(LR == 3) return 1;
  if(LR == -3) return -1;
  return 0;
}

int TriggerRoad::getTBRevH1LR(int shiftN)
{

    GeomSvc* p_geomSvc = GeomSvc::instance();
    int LR_L = 0;
    int LR_R = 0;
    int LR=0;
    int TB = 0;

    for(int i = 0; i < 4; i++)
      {
	if((detectorIDs[i] & 1) == 0)
	  {
	    TB += 1;
	  }
	else
	  {
	    TB += -1;
	  }
      }
    if ((TB!=4) && (TB!=-4)) return 0;

    //  cout <<"H1:"<< elementIDs[0] <<" middle:"<< p_geomSvc->getPlaneNElements(detectorIDs[0])/2<< endl;

    if(elementIDs[0] <= (p_geomSvc->getPlaneNElements(detectorIDs[0])/2) + shiftN)  //middle H1elementID <=11
      {
	LR_R += 1; //right
      }
    if(elementIDs[0] >= (p_geomSvc->getPlaneNElements(detectorIDs[0])/2) - shiftN)  //middle H1elementID <=11
      {
	LR_L += 1;  //left
      }

    for(int i = 1; i < 4; i++)
      {
	if(elementIDs[i] <= p_geomSvc->getPlaneNElements(detectorIDs[i])/2)
	  {
	    LR += 1;  //right
	  }
	else
	  {
	    LR += -1;  //left
	  }
      }



    if( ( LR_L==1) && (LR == 3)) return 1;  //H1 left, H234 right
    if( (LR_R==1) && (LR ==-3)) return -1;  //H1 right, H234 left
    return 0;
}

int TriggerRoad::getBigH1LR()
{
  GeomSvc* p_geomSvc = GeomSvc::instance();

  int LR = 0;
  for(int i = 1; i < 4; i++)
    {
      if(elementIDs[i] <= p_geomSvc->getPlaneNElements(detectorIDs[i])/2)
	{
	  LR += 1; //right
	}
      else
	{
	  LR += -1; //left
	}
    }

  if(LR == 3) return 1;
  if(LR == -3) return -1;
  return 0;
}


int TriggerRoad::getTB()
{
  int TB = 0;
  for(int i = 0; i < 4; i++)
    {
      if((detectorIDs[i] & 1) == 0)
	{
	  TB += 1;
	}
      else
	{
	  TB += -1;
	}
    }

  if(TB == 4) return 1;
  if(TB == -4) return -1;
  return 0;
}

int TriggerRoad::getBigH1TB()
{
  int TB = 0;
  for(int i = 1; i < 4; i++)
    {
      if((detectorIDs[i] & 1) == 0)
	{
	  TB += 1;
	}
      else
	{
	  TB += -1;
	}
    }

  if(TB == 3) return 1;
  if(TB == -3) return -1;
  return 0;
}

bool Trigger::operator==(const Trigger& elem) const
{
  if(p_road.roadID != elem.p_road.roadID) return false;
  if(m_road.roadID != elem.m_road.roadID) return false;

  return true;
}
